Skip to content

Add sleep support to win32 event loop#10605

Merged
straight-shoota merged 7 commits intocrystal-lang:masterfrom
straight-shoota:feature/win32-event_loop-stub
Sep 14, 2021
Merged

Add sleep support to win32 event loop#10605
straight-shoota merged 7 commits intocrystal-lang:masterfrom
straight-shoota:feature/win32-event_loop-stub

Conversation

@straight-shoota
Copy link
Copy Markdown
Member

This patch enhances the event loop for win32 to support sleeping fibers.
The implementation relies on Sleep for now. It will be replaced by a timeout with GetQueuedCompletionStatus in a subsequent step (see #9957 (comment)).

@straight-shoota straight-shoota added kind:feature topic:stdlib:concurrency platform:windows Windows support based on the MSVC toolchain / Win32 API labels Apr 5, 2021
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
Comment on lines +71 to +85
chan = Channel(Int32).new
spawn do
3.times do |i|
sleep 40.milliseconds
chan.send (i + 1)
end
end
spawn do
2.times do |i|
sleep 100.milliseconds
chan.send (i + 1) * 10
end
end

Array(Int32).new(5) { chan.receive }.should eq [1, 2, 10, 3, 20]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This newly added spec fails on darwin. I've already increased the sleep times trying to avoid inconsistencies.
Could someone run this example on macOS to find a sleep amount that leads to success consistently? Or investigate why if it continues to fail? :D

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm... We need this in order to get the final approval, otherwise the macOS test will start failing on master.

Copy link
Copy Markdown
Contributor

@yxhuvud yxhuvud May 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why it fails? Those times indicate some pretty insane overhead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why. We need someone to test this on macOS manually.

But I'll disable this spec on darwin for now, to keep CI happy.

sleep_time = next_event.wake_at - Time.monotonic

if sleep_time > Time::Span.zero
LibC.Sleep(sleep_time.total_milliseconds)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This can only work when all events are sleeps, right?

Otherwise a completion could happen but not be received until the sleep is over. I think the fetch completion command has a timeout that can be used for the case when there is both completions and sleeps happening. But perhaps actual sleep would still be necessary in the case where there are no completions, that depends on how the fetching command behave if empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this will later be changed to GetQueuedCompletionStatus. See comment in the OP. For now, Sleep is good enough until we can introduce completion ports properly (this is a preparatory step for that).

@straight-shoota straight-shoota added this to the 1.2.0 milestone Sep 13, 2021
@straight-shoota straight-shoota merged commit db171c2 into crystal-lang:master Sep 14, 2021
@straight-shoota straight-shoota deleted the feature/win32-event_loop-stub branch September 14, 2021 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API pr:needs-review topic:stdlib:concurrency

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants